package com.hongxin.nw.netty.down;

import com.hongxin.nw.netty.model.Transfer;
import com.hongxin.nw.netty.model.analysis.info.Mrena1_17_1;
import com.hongxin.nw.netty.model.packing.ASDUcsdna1;
import com.hongxin.nw.netty.model.packing.info.Csdna1_16;
import com.hongxin.nw.netty.utils.FieldPack;
import com.hongxin.nw.netty.utils.pools.AsduStartPools;
import com.hongxin.nw.netty.values.StaticValue;
import com.hongxin.nw.netty.values.TypeIdentif;
import lombok.extern.slf4j.Slf4j;

/**
 * @remark 充电过程中上传数据确认
 * @author: GuoXiaolin
 * @date 2019/6/21 17:25
 * @Version 1.0
 * @Descibe
 */
@Slf4j
public class ChargeProcessConfirmReq implements IChannelRequest {
    @Override
    public String doPacking(Transfer transfer) throws Exception {

        String code = transfer.getCode();
        Mrena1_17_1 dataItem = (Mrena1_17_1)transfer.getObj();
        Csdna1_16 csdna1_16 = new Csdna1_16();
        csdna1_16 = csdna1_16.setCo(code)
                .setChargeIdentify(dataItem.getChargeIndex())
                .setCard("123456")
                .setMoney(0.00)
                .setAccountMoney(0.00)
                .setSuccessIdentify(StaticValue.E_SUCCESS)
                .setFailReason("0000")
                .setCcLc(0.00)
                .setSyLc(0.00)
                .setCcDl(0.00)
                .setSyDl(0.00)
                .setCcCs(0.00)
                .setSyCs(0.00);

        ASDUcsdna1 mm = new ASDUcsdna1();
        mm.setLogType(String.valueOf(TypeIdentif.RECORD_UPLOAD_CONFIRM.getInt()));
        mm.setSendData(csdna1_16.packing());

//        as.setType(TypeIdentif.IDENTIFY_C_SD_NA_1.getInt());
//        as.setVsq(StaticValue.DEFAULT_VSQ, StaticValue.SQ0);
//        as.setCot(TypeIdentif.COT_UNKNOWN_COT.getInt());
//        as.setInfoadr(Integer.valueOf(dataItem.getChargeIndex()));
//        as.setStaadr(AsduStartPools.getAsduStart(code).getStatAddress());

        String as = FieldPack.packASDU(TypeIdentif.IDENTIFY_C_SD_NA_1.getInt(),
                TypeIdentif.COT_UNKNOWN_COT.getInt(),
                AsduStartPools.getAsduStart(code).getStatAddress(), Integer.valueOf(dataItem.getChargeIndex()),
                StaticValue.DEFAULT_VSQ, StaticValue.SQ0);

        String data = as + mm.getHexData();

//        APCI ac = new APCI();
//        ac.setIdentifyStart(TypeIdentif.IDENTIFY_START.getInt())
//                .setApduLength(as.getHexData().length()/2)
//                .setContarea(TypeIdentif.FORMAT_I.getStr(),StaticValue.U_START_V);
//        return ac.getHexData() + as.getHexData();

        String ac = FieldPack.packAPCI(data.length(), TypeIdentif.FORMAT_I.getStr(), StaticValue.U_START_V);
        return ac + data;
    }
}
